package connect

import (
	"encoding/json"

	"gitee.com/yutiandou/wdtools/wdlog"
)

//Warn 错误反馈
type Warn struct {
	Error int         `json:"error"`
	Data  interface{} `json:"data"`
}

var warn_def []byte
var Warn_node_offset []byte
var Success_Connect []byte
var Warn_Server_fail []byte
var HeartBeat []byte

func init() {

	warn_def = newWarn(500, "server fail")
	Warn_node_offset = newWarn(400, "request offset fail")
	Success_Connect = newWarn(200, "success")
	Warn_Server_fail = newWarn(404, "purpose not found")
	HeartBeat = newWarn(100, "HeartBeat")
}

func newWarn(stat int, content string) []byte {
	w := &Warn{
		Error: stat,
		Data:  content,
	}
	dsc, err := json.Marshal(w)
	if wdlog.Error(err) {
		return nil
	}
	return dsc
}

func NewWarn(errid int, data string) *Warn {
	return &Warn{
		Error: errid,
		Data:  data,
	}
}

func (w *Warn) ToByte() []byte {
	data, err := json.Marshal(w)
	if wdlog.Error(err) {
		return warn_def
	}
	return data
}
